package com.example.controller;

import com.example.dto.UserDTO;
import com.example.dto.UserRegisterDTO;
import com.example.pojo.*;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param registerDTO
     * @return
     */
    @PostMapping("/user/register")
    public Result register(@RequestBody UserRegisterDTO registerDTO) {
        log.info("用户注册，参数：{}", registerDTO);
        return userService.register(registerDTO);
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/user/login")
    public Result login(@RequestBody User  user) {
        log.info("用户登录，参数：{}", user);
        LoginInfo loginInfo = userService.login(user);
        if (loginInfo != null) {
            log.info("用户登录成功，返回登录信息,token:{}", loginInfo.getToken());
            return Result.success(loginInfo);
        }
        return Result.error("账号或密码错误");
    }

    /**
     * 分页查询用户
     * @param userQueryParam
     * @param roleCode
     * @return
     */
    @GetMapping("/users")
    public Result pageQuery(UserQueryParam userQueryParam, @RequestAttribute("currentRole") String roleCode) {
        log.info("分页查询用户，userQueryParam：{},roleCode：{}", userQueryParam, roleCode);
        Integer roleCodeId = getRoleCodeIdByRoleCode(roleCode);
        PageResult<User> pageResult = userService.pageQuery(userQueryParam,roleCodeId);
        log.info("分页查询用户，返回结果：{}", pageResult);
        if (pageResult == null) {
            return Result.error("没有查询到用户");
        }
        return Result.success(pageResult);
    }
    /**
     * 获取角色权限Id
     * @param roleCode
     * @return
     */
    private static Integer getRoleCodeIdByRoleCode(String roleCode) {
        Integer roleCodeId = 2;
        if ("admin".equalsIgnoreCase(roleCode)) {
            roleCodeId = 3;
        } else if ("super_admin".equalsIgnoreCase(roleCode)) {
            roleCodeId = 1;
        } else {
            roleCodeId = 2;
        }
        return roleCodeId;
    }

    /**
     * 查询用户
     * @param userId
     * @param roleCode
     * @return
     */
    @GetMapping("/user/{userId}")
    public Result getUserByuserId(@PathVariable Long userId , @RequestAttribute("currentRole") String roleCode) {
        log.info("要查询的用户的用户的userId：{}", userId);
        Integer roleCodeId = getRoleCodeIdByRoleCode(roleCode);
        log.info("使用查询用户功能的人的角色码roleCodeId：{}", roleCodeId);
        User user = userService.getUserByuserId(userId,roleCodeId);
        if (user == null) {
            return Result.error("没有查询到用户");
        }
        return Result.success(user);
    }
    /**
     * 更新用户
     * @param userId
     * @param user
     * @return
     */
    @PutMapping("/user/{userId}")
    public Result updateUser(@PathVariable Long userId, @RequestBody User user,@RequestAttribute("currentRole") String roleCode) {
        log.info("更新用户，userId：{},user：{}", userId, user);
        Integer roleCodeId = getRoleCodeIdByRoleCode(roleCode);
        log.info("更新用户，roleCodeId：{}", roleCodeId);
        String updatemsg = userService.updateUser(userId, user,roleCodeId);
        log.info("更新用户，返回结果：{}", updatemsg);
        if ("更新成功".equals(updatemsg)) {
            log.info("更新用户成功");
            return Result.success(updatemsg);
        }
        return Result.error(updatemsg);

    }
    /**
     * 重置密码
     * @param userDTO
     * @return
     */
    @PostMapping("/user/reset-password")
    public Result resetPassword(@RequestBody UserDTO userDTO,@RequestAttribute("currentRole") String roleCode) {
        Integer roleCodeId = getRoleCodeIdByRoleCode(roleCode);
        log.info("重置密码，roleCodeId：{}", roleCodeId);
        String resetPasswordMsg = userService.resetPassword(userDTO,roleCodeId);
        log.info("重置密码，返回结果：{}", resetPasswordMsg);
        if ("重置密码成功".equals(resetPasswordMsg)) {
            log.info("重置密码成功");
            return Result.success(resetPasswordMsg);
        }
        return Result.error(resetPasswordMsg);
    }
}
